#!/bin/bash

OUT=out

function applyPatch () {
	app_name=$1
	app_dir=$2

	for patch in `find $app_name -name "*.patch"`
	do
		smali_file=`cat $patch | grep "^---" | sed "s@$app_name\/@$app_name#@" | cut -d"#" -f2 | awk -F "\t" '{print $1}'`
		patch $app_dir/$smali_file $patch

			for rej in `find $app_dir -name "*.rej"`
			do
				echo "Patch $patch failed!!"
				exit 1
			done
	done
}

if [ $1 = "SamsungCamera3" ];then

	echo "fix SamsungCamera3"

	for file in `find $2 -name "*.smali"`
	do
		sed -i 's#\"com\.sec\.android\.gallery3d\"#\"com\.meizu\.media\.gallery\"#g' $file
		sed -i 's#\"com\.sec\.android\.gallery3d\.app\.Gallery\"#\"com\.meizu\.media\.gallery\.GalleryActivity\"#g' $file
	done

fi

if [ $1 = "TeleService" ];then
	applyPatch $1 $2
fi

if [ $1 = "SystemUI" ];then
	applyPatch $1 $2
	for file in `find $2 -name "*.smali"`
	do
		sed -i 's#\"com\.meizu\.media\.camera\"#\"com\.sec\.android\.app\.camera\"#g' $file
		sed -i 's#\"com\.meizu\.media\.camera\.CameraLauncher\"#\"com\.sec\.android\.app\.camera\.Camera\"#g' $file
	done
fi

if [ $1 = "MzCallSetting" ];then
	applyPatch $1 $2
fi

if [ $1 = "Telecom" ];then
	applyPatch $1 $2
fi

if [ $1 = "Gallery" ];then

	echo "fix sdcard path"
	for file in `find $2 -name "SDCardHelper.smali"`
	do
		sed -i 's/sdcard1/extSdCard/g' $file
	done

fi

if [ $1 = "MediaProvider" ];then

	echo "fix sdcard path"
	for file in `find $2 -name "MediaProvider.smali"`
	do
		sed -i 's/sdcard1/extSdCard/g' $file
	done

fi

if [ $1 = "FileManager" ];then

	echo "fix sdcard path"
	for file in `find $2 -name "*.smali"`
	do
		sed -i 's/sdcard1/extSdCard/g' $file
	done

fi

if [ $1 = "FlymeLauncher" ];then

	echo "fix statusbar"
	sed -i '/<uses-permission android:name="android.permission.STATUS_BAR"\/>/a\<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"\/>' $2/AndroidManifest.xml

fi

#if [ $1 = "PackageInstaller" ];then
#	applyPatch $1 $2
#	echo "fix black"
#	sed -i 's/ActivityFirstLoadTheme/MyBaseTheme/g' $2/AndroidManifest.xml
#fi
